// $(function () {
//     // 全局变量
//     let token = 'a'.repeat(32) + 1
//     // 购物车列表
// }) 
    //购物车列表生成
    // 封装总价计算函数
    function sum(){
        let totalPrice = 0
        let vipPrice = 0
        $('tr>.title').each((index,item)=>{
            totalPrice += parseFloat($(item).parent().find('.tPrice').text().substr(1)*100)
            $('.price .total-price').html(`
            应付金额：<span><i>¥</i><b>${totalPrice/100}</b></span>
            `)
            vipPrice+= parseFloat($(item).parent().find('.vip').text().substr(1)*100*$(item).parent().find('.input').val())
            $('.vip span').html(`
            会员价格：<span><i>¥</i><b>${vipPrice/100}</b></span>
            `)
        })
    }

    let totalPrice = 0
    if(!token){
        cocoMessage.error("请先完成登录");
        // swal({title:`请先完成登录`,text: "美团提示!",icon: "error",button: "确定!",});
      }
      $.get('/api/carts/index.jsp',{token},res=>{
        console.log(res);
        $('table>tbody').text('')
        let html = `
              <tr>
                <th class="desc">项目</th>
                <th class="unit-price">单价</th>
                <th class="vip-price">vip价</th>
                <th class="num">数量</th>
                <th class="total-price">总价</th>
                <th class="delete" style="width:40px">操作</th>
              </tr>
              `
        
        $.each(res.data,(index,item)=>{
          let tPrice = parseInt(item.market_price*100*item.goods_number)/100
          totalPrice+=tPrice
          html+=`
          <tr data_id ="${item.goods_id}">
                <td class="title">
                  <a href="./waimai-detail.html?store_id=${item.store_id}">${item.store_name}：${item.goods_name}</a>
                  <p>${item.goods_attr}</p>
                </td>
                <td>¥${item.market_price}</td>
                <td class="vip" style="color:red;">¥${item.shop_price}</td>
                <td>
                  <div class="input-number">
                    <button class="minus-btn" type="button">-</button>
                    <input type="text" class="input" value="${item.goods_number}">
                    <button class="plus-btn" type="button">+</button>
                  </div>
                </td>
                <td class="tPrice">¥${tPrice}</td>
                <td><a class="delete" href="javascript:;">删除</a></td>
              </tr>
          `
        })
        $('table>tbody').html(html)
        sum()
      },'json')

        // 删除
    $('tbody').on('click','.delete',function(){
        let goods_id = $(this).parents('tr').attr('data_id')
        // 发送异步请求删除tr标签
        $.get('/api/carts/delete.jsp',{
          token,
          goods_id
        },res=>{
          console.log(res);
          if(res.meta.state!=200){
            cocoMessage.error(res.meta.msg);
            //  swal({title:res.meta.msg,text: "美团提示!",icon: "error",button: "确定!",});
          }else{
            //静态页面删除tr标签
            $(this).parents('tr').remove()
            sum()
          }
        },'json')
      })

       // 加一
  $('tbody').on('click','.plus-btn',function(){
    let num = $(this).prev().val()
    num++
    // 判断最大的整数防止bug
    if(num>=9007199254740991) num = 9007199254740991
    let goods_id = $(this).parents('tr').attr('data_id')
    // console.log(token);
    $.post('/api/carts/update.jsp',{token,goods_id,buyNum:num},res=>{
      console.log(res);
      if(res.meta.state===200) {
        location.reload()
      }else{
        if(res.meta.msg=='库存不足'){
          msg= `${res.data.goods_name}:${res.meta.msg} 剩余库存${res.data.total_number}`
        }else{
          msg = res.meta.msg
        }
        cocoMessage.error(msg);
        // swal({title:msg,text: "美团提示!",icon: "error",button: "确定!",});
        setTimeout(()=>{
          location.reload()
        },2000)
      }
    },'json')
  })

//   减一
$('tbody').on('click','.minus-btn',function(){
    let num = $(this).next().val()
    num--
    // 判断最大的整数防止bug
    if(num<1){
        cocoMessage.error("至少购买一件");
        // swal({title:'至少购买一件',text: "美团提示!",icon: "error",button: "确定!",});
        return
    }
    let goods_id = $(this).parents('tr').attr('data_id')
    // console.log(token);
    $.post('/api/carts/update.jsp',{token,goods_id,buyNum:num},res=>{
      console.log(res);
      if(res.meta.state===200) {
        location.reload()
      }else{
        cocoMessage.error(res.meta.msg);
        // swal({title:res.meta.msg,text: "美团提示!",icon: "error",button: "确定!",});
      }
    },'json')
  })

  //值改变
  $('tbody').on('input','.input',function(){
      let num = $(this).val()
      num = num.replace(/\D/g,'')
      if(!num || num<1) num=1
      $(this).val(num)
  })

//   失焦事件
$('tbody').on('blur','.input',function(){
    let num = $(this).val()
    num = num.replace(/\D/g,'')
    if(!num || num<1) num=1
      // 判断最大的整数防止bug
      // if(num>=9007199254740991) num = 9007199254740991
      let goods_id = $(this).parents('tr').attr('data_id')
      // console.log(token);
      $.post('/api/carts/update.jsp',{token,goods_id,buyNum:num},res=>{
      if(res.meta.state===200) {
          location.reload()
      }else{
          if(res.meta.msg=='库存不足'){
              // swal({title:`${res.data.goods_name}:${res.meta.msg} 剩余库存${res.data.total_number}`,text: "美团提示!",icon: "error",button: "确定!",});
              cocoMessage.error(`${res.data.goods_name}:${res.meta.msg} 已重置为最大库存`);
            //   swal({title:`${res.data.goods_name}:${res.meta.msg} 已重置为最大库存`,text: "美团提示!",icon: "error",button: "确定!",});
              $.post('/api/carts/update.jsp',{token,goods_id,buyNum:res.data.total_number},res=>{
                setTimeout(()=>{
                    location.reload()
                },1000)
                },'json')
          }else{
            cocoMessage.error(res.meta.msg);
            //   swal({title:res.meta.msg,text: "美团提示!",icon: "error",button: "确定!",});
          }
      }
      },'json')

})
     